package cn.nawang.ebeim.ukey;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Ukey {
  public static final String UKEY_LIBRARY = "ukeys";
  private static final String UKEY_PASSWORD = "888888";
  public static final int SERIAL_LEN = 33;
  public static final int RETRY_MAX = 6;

  private static final String UKEYS = "ukeys";
  private static final String UKEYS_DLL = "ukeys.dll";
  private static final String UKEYS_DLL_32 = "ukeys_x32.dll";
  private static final String UKEYS_DLL_64 = "ukeys_x64.dll";
  private static final String UKEYS_DRIVER = "xjkKeyMana.dll";
  private static final String UKEYS_DRIVER_32 = "xjkKeyMana_x32.dll";
  private static final String UKEYS_DRIVER_64 = "xjkKeyMana_x64.dll";
  /*
   * static { System.loadLibrary(UKEY_LIBRARY); }
   */

  private static String getInstallPath(String libs) {
    String[] strs = libs.split(";");
    if (strs.length > 0) {
      return strs[0];
    }
    return null;
  }

  private static boolean installLibrary() {
    String root = new Ukey().getClass().getResource("/").getPath();
    String libs = System.getProperty("java.library.path");
    String lib = getInstallPath(libs);
    if (lib == null) {
      return false;
    }
    String arch = System.getProperty("os.arch");
    System.out.println(lib);
    if (arch.toLowerCase().equals("amd64")) {
      copyFile(root, lib, UKEYS_DLL_64, UKEYS_DLL);
      copyFile(root, lib, UKEYS_DRIVER_64, UKEYS_DRIVER);
    } else {
      copyFile(root, lib, UKEYS_DLL_32, UKEYS_DLL);
      copyFile(root, lib, UKEYS_DRIVER_32, UKEYS_DRIVER);
    }

    return true;
  }

  private static void copyFile(String srcPath, String dstPath, String sname, String dname) {
    String src = srcPath + File.separator + sname;
    String dst = dstPath + File.separator + dname;

    File dstFile = new File(dst);
    if (dstFile.exists()) {
      return;
    }

    File srcFile = new File(src);
    if (!srcFile.exists()) {
      System.out.print("File doesn't exist:" + src);
      return;
    }

    copyFile(srcFile, dstFile);
  }

  private static void copyFile(File src, File dst) {
    try {
      FileInputStream fin = new FileInputStream(src);
      FileOutputStream fout = new FileOutputStream(dst);
      byte[] bt = new byte[1024];
      int c;
      while ((c = fin.read(bt)) > 0) {
        fout.write(bt, 0, c);
      }
      fin.close();
      fout.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void loadLibrary() {
    if (installLibrary()) {
      System.loadLibrary(UKEYS);
    }
  }

  public native static boolean xjkOpenKeyEx(byte[] btPassWord);

  public native static boolean xjkGetSerial(byte[] cSerial);

  public static String getSerialIdImpl() {
    String id = null;
    if (Ukey.xjkOpenKeyEx(UKEY_PASSWORD.getBytes())) {
      byte[] serial = new byte[SERIAL_LEN];
      if (Ukey.xjkGetSerial(serial)) {
        id = new String(serial).trim();
      }
    }
    return id;
  }

  public static String getSerialId() {
    String id = null;
    for (int i = 0; i < RETRY_MAX; i++) {
      id = getSerialIdImpl();
      if (id != null) {
        break;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return id;
  }

  public static void main(String[] args) throws InterruptedException {
    Ukey.loadLibrary();
    // System.loadLibrary(UKEYS);
    // System.out.println(System.getProperty("java.library.path"));
    /*
     * if (Ukey.xjkOpenKeyEx(UKEY_PASSWORD.getBytes())) { byte[] serial = new byte[33]; if
     * (Ukey.xjkGetSerial(serial)) { String ser = new String(serial).trim();
     * System.out.println(ser); } else { System.out.println("get serial id failed"); } } else {
     * System.out.println("open ukey failed"); }
     */
    // System.out.println(Ukey.getSerialId(6));
    while (true) {
      System.out.println(Ukey.getSerialId());
      Thread.sleep(1000);
    }
  }

}
